package cn.kgc.day04.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 课工场
 * @date 2024/10/11
 * @description /student
 * @WebServlet("/student/list") 在类上添加@WebServlet注解可以替代 标签的请求映射方案    参数   请求映射路径  /student/list
 * <p>
 * 如何通过一个servlet实现对该模块的所有操作
 * <p>
 * 列表    /student/list
 * 添加    /student/add
 * 修改    /student/edit
 * 删除   /student/delete
 * <p>
 * 解决方案 :
 * 1. 请求方式   /student  get 查询    post 添加   delete 删除   put 修改  restful设计风格
 * 2. 请求参数
 *      添加   /student?method=addStudent
 *      删除   /student?method=deleteStudent
 *      修改   /student?method=editStudent
 *      查询   /student?method=selectStudent
 * 3. 反射实现方法的调用  推荐
 */
@WebServlet("/student")
public class StudentServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //  1. 获取请求参数
        String method = req.getParameter("method");

        if ("addStudent".equals(method)) {
            addStudent(req, resp);
        } else if ("selectStudent".equals(method)) {
            selectStudent(req, resp);
        } else if ("deleteStudent".equals(method)) {
            deleteStudent(req, resp);
        } else if ("editStudent".equals(method)) {
            editStudent(req, resp);
        }


    }

    // 添加
    protected void addStudent(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("student add");
    }

    // 查询
    protected void selectStudent(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("student select");
    }

    // 删除
    protected void deleteStudent(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("student delete");
    }

    // 修改
    protected void editStudent(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("student edit");
    }


}
